package com.itheima.wemedia.controller;

import com.itheima.admin.pojo.AdChannel;
import com.itheima.common.vo.PageResultVo;
import com.itheima.common.vo.ResultVo;
import com.itheima.wemedia.dto.AuthReqDto;
import com.itheima.wemedia.dto.WmNewsPageReqDto;
import com.itheima.wemedia.pojo.WmMaterial;
import com.itheima.wemedia.pojo.WmNews;
import com.itheima.wemedia.pojo.WmUser;
import com.itheima.wemedia.service.ApiService;
import com.itheima.wemedia.service.WmMaterialService;
import com.itheima.wemedia.service.WmNewsService;
import com.itheima.wemedia.service.WmUserService;
import com.itheima.wemedia.vo.WmNewsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ApiController {
    @Autowired
    private WmUserService wmUserService;

    @Autowired
    private WmMaterialService wmMaterialService;

    @Autowired
    private WmNewsService wmNewsService;

    @Autowired
    private ApiService apiService;

    /**
     * 远程调用添加自媒体账号
     * @param wmUser
     * @return
     */
    @PostMapping("/api/wnUser/add")
    public ResultVo<WmUser> add(@RequestBody WmUser wmUser){
        wmUserService.save(wmUser);
        return ResultVo.ok();
    }

    /**
     * 通appUserId查询自媒体账号信息
     * @param appUserId
     * @return
     */
    @GetMapping("/api/wnUser/{appUserId}")
    public ResultVo<WmUser> getByAppUserId(@PathVariable(name = "appUserId")Integer appUserId){
     WmUser wmUser = wmUserService.getByAppUserId(appUserId);
     return ResultVo.ok(wmUser);
    }

    @PostMapping("/api/wmMaterial/add")
    public ResultVo add(@RequestBody WmMaterial wmMaterial){
        wmMaterialService.save(wmMaterial);
        return ResultVo.ok();
    }

    @GetMapping("/channel/channels")
    public ResultVo<List<AdChannel>> listAll(){
         List<AdChannel> list = apiService.fainAll();
          return ResultVo.ok(list);
    }

    /**
     * 通过id查询文章信息
     * @return
     */
    @GetMapping("/api/wmNews/{newsId}")
    public ResultVo<WmNews> getWmNewsById(@PathVariable(value = "newsId")Long id){
        WmNews wmNews = wmNewsService.getById(id);
        return ResultVo.ok(wmNews);
    }

    /**
     * 通过id更新文章信息
     * @return
     */
    @PutMapping("/api/wmNews/update")
    public ResultVo update(@RequestBody WmNews wmNews){
        wmNewsService.updateById(wmNews);
        return ResultVo.ok();
    }

    @PostMapping("/news/list_vo")
    public PageResultVo findPage4Admin(@RequestBody WmNewsPageReqDto dto){
       PageResultVo vo = wmNewsService.findPage4Admin(dto);
        return vo;
    }

    /**
     * 人工审核-通过
     * @param dto
     * @return
     */
    @PostMapping("/news/auth_pass")
    public ResultVo authPass(@RequestBody AuthReqDto dto){
        wmNewsService.authPass(dto);
        return ResultVo.ok();
    }

    /**
     * 人工审核-驳回
     * @param dto
     * @return
     */
    @PostMapping("/news/auth_fail")
    public ResultVo authFail(@RequestBody AuthReqDto dto){
        wmNewsService.authFail(dto);
        return ResultVo.ok();
    }

    /**
     * 文章详情查看
     * @param id
     * @return
     */
    @GetMapping("/news/one_vo/{id}")
    public ResultVo<WmNewsVo> getById(@PathVariable(value = "id")Long id){
      WmNewsVo wmNewsVo = wmNewsService.findById(id);
      return ResultVo.ok(wmNewsVo);
    }

    /**
     * 查询审核通过的文章，状态为8的。进行同步到文章微服务
     * @param dto
     * @return
     */
    @PostMapping("/api/wmNews/list4ArticleSync")
    public ResultVo<List<WmNewsVo>> list4ArticleSync(@RequestBody WmNewsPageReqDto dto){
       List<WmNewsVo> voList = wmNewsService.list4ArticleSync(dto);
       return ResultVo.ok(voList);
    }
}
